package com.iplay.实战java高并发程序设计.chapter5.c54;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;

import java.nio.ByteBuffer;
import java.util.concurrent.ThreadFactory;

/**
 * @author dangwu
 * @since 2021/11/5 3:29 下午
 */
public class TestPC {
    public static void main(String[] args) throws InterruptedException {
        PCDataFactory pcDataFactory = new PCDataFactory();
        ThreadFactory threadFactory = ThreadFactoryBuilder.create().build();
        //必须指定ring buffer的大小为2的幂次方
        int bufferSize = 1024;
        Disruptor<PCData> disruptor = new Disruptor<>(pcDataFactory, bufferSize, threadFactory);
        disruptor.handleEventsWithWorkerPool(new Consumer(), new Consumer(), new Consumer(), new Consumer());
        disruptor.start();
        RingBuffer<PCData> ringBuffer = disruptor.getRingBuffer();
        Producer producer = new Producer(ringBuffer);
        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long i = 0; true; i++) {
            bb.putLong(0,i);
            producer.pushData(bb);
            Thread.sleep(100);
            System.out.println("add data "+ i);
        }
    }
}
